<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>数据库初始化配置</title>
    <style>
        * {
            margin: 0;
            padding: 0;
        }

        .box-ok {
            width: 100%;
            padding: 20px 0;
            color: rgba(0, 0, 0, .6);
            text-align: center;
            display: none;
        }

        .box-ok:nth-child(2n+1) {
            background-color: rgba(173, 194, 194, 0.4);
        }

        .box-ok:nth-child(2n+2) {
            background-color: rgba(253, 253, 253, 0.6);
        }

        body .box-login:nth-child(n) {
            background-color: transparent;
        }

        .box-login a {
            text-decoration: none;
            display: inline-block;
            padding: 7px 10px;
            border-radius: 6px;
            color: white;
            margin: 0 10px;
            background-color: rgba(0, 179, 45, 0.89);
        }
    </style>
    <script src="https://cdn.bootcss.com/jquery/3.2.1/jquery.js"></script>
    <script>
        $(function() {
            // 动画效果
            $(".box-ok").fadeToggle(1000, "swing");
        })
    </script>
</head>

<body>

<?php
    header("Content-type:text/html;charset=utf-8");
    if ($_POST) {
        $POS = array();
        $a = 1;
        $POS["mysqlurl"]  = $_POST["mysqlurl"];
        $POS["mysqluser"]  = $_POST["mysqluser"];
        $POS["mysqlpss"]  = $_POST["mysqlpss"];
        $string = file_get_contents("config.php");
        foreach ($POS as $k => $v) {
            //定义正则来查找内容，这里面的key为form表单里面的name
            $yx = "/define\('$k','.*?'\);/";
            //将内容匹配成对应的key和修改的值
            $re = "define('$k','$v');";
            //替换内容
            $string = preg_replace($yx, $re, $string);
        }
        file_put_contents('config.php', $string);

        echo'<div class="box-ok"><p>参数储存成功！</p></div>';

    }

    header("Content-type:text/html;charset=utf-8");

    require_once("config.php");

    $cin = mysqli_connect(mysqlurl, mysqluser, mysqlpss);

    if (!$cin) {
        die('数据库连接失败: 请查看数据库账户密码是否正确' . mysqli_connect_error());
    }else{
        echo '<div class="box-ok"><p>MySQL连接成功！</p></div>';
    }

    $mysqlopen = "CREATE DATABASE IF NOT EXISTS hongbao  DEFAULT CHARSET utf8 COLLATE utf8_general_ci";

    if (mysqli_query($cin,$mysqlopen)) {
        $b = 1;
        echo '<div class="box-ok"><p>数据库创建成功！</p></div>';
    }else{
        echo '<div class="box-ok"><p>数据库创建失败！</p></div>';
    }

    mysqli_select_db($cin,"hongbao");

    $sqladmin = "CREATE TABLE userinfo (
        id int NOT NULL primary key AUTO_INCREMENT,
        title varchar(15) NOT NULL,
        userid varchar(30) NOT NULL,
        username varchar(50) NOT NULL,
        theme varchar(30) NOT NULL,
        coding varchar(30) NOT NULL,
        createtime varchar(30) NOT NULL,
        problem varchar(1000) NOT NULL,
        results varchar(1000) NOT NULL
    )";


    $sqltable = mysqli_query($cin, $sqladmin);

    if(!$sqltable){
        echo '<div class="box-ok"><p>字段创建失败！</p></div>';
    }else{
        echo '<div class="box-ok"><p>字段创建成功！</p></div>'; 
    }
    if ($a && $b) {
        echo '<div class="box-ok"><p>程序安装已完成，快开启您的答题之旅吧！</p></div><div class=" box-ok box-login"><a href="../index.html">网站首页</a></div>';
        unlink("index.php");
    };
?>

</body>
</html>